//
//  MemeCreatorViewController.m
//  reacticons
//
//  Created by Nong Nghia on 7/30/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "MemeCreatorViewController.h"
#import "Utilities.h"
#define DONE_BTN_TAG 1
#define CANCEL_BTN_TAG 2

@interface MemeCreatorViewController ()

@end

@implementation MemeCreatorViewController
@synthesize image;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [scrollView setScrollEnabled:YES];
    [self.navigationController.navigationBar setBarStyle:UIBarStyleBlack];
    [imageView setImage:image];
    [imageView setContentMode:UIViewContentModeScaleAspectFit];
    toptextStr=[@"" retain];
    bottextStr=[@"" retain];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


-(IBAction)clickedBtn:(id)sender{
    NSLog(@"%d",[sender tag]);
    
    if ([sender tag]==DONE_BTN_TAG) {
        [Utilities saveImage:imageView.image 
         toDocumentSubFolder:@"Meme" 
                    withName:[NSString stringWithFormat:@"%@_%@_MEME.png",
                              [Utilities uniqueStringWithPrefix:@"M"],
                              [Utilities uniqueStringWithPrefix:@""]]];
        
        [self dismissModalViewControllerAnimated:YES];

    }
    if ([sender tag]==CANCEL_BTN_TAG) {
        [self dismissModalViewControllerAnimated:YES];
    }
}

#pragma mark - UITextField Delegate

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    saveBtn.enabled=NO;
    [UIView beginAnimations:@"showkeyboard" context:NULL]; // Begin animation

    [scrollView setContentSize:CGSizeMake(320, 460+216-44)];
    if (textField==botText) {
        [scrollView scrollRectToVisible:CGRectMake(0, botText.frame.origin.y+botText.frame.size.height+216-460+44+5, 320, 460-44) animated:YES];
    }
    [UIView commitAnimations]; // End animations    

}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [UIView beginAnimations:@"hidekeyboard" context:NULL]; // Begin animation
    [scrollView setContentSize:CGSizeMake(320, 460-44)];
    [UIView commitAnimations]; // End animations  
    

    return [textField resignFirstResponder];
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    saveBtn.enabled=YES;
    [imageView setImage:[Utilities createMeme:bottextStr withImage:image atTop:NO]];
    [imageView setImage:[Utilities createMeme:toptextStr withImage:imageView.image atTop:YES]];
    
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString *text=@"";
    if (range.location+range.length <= textField.text.length) {
        text= [textField.text stringByReplacingCharactersInRange:range withString:string];

    }
    
    if (textField==botText) {
        bottextStr=[[NSString stringWithString:text] retain];
//        [imageView setImage:[Utilities createMeme:bottextStr withImage:image atTop:NO]];
//        [imageView setImage:[Utilities createMeme:toptextStr withImage:imageView.image atTop:YES]];

    }
    if (textField==topText) {
        toptextStr=[[NSString stringWithString:text] retain];
//        [imageView setImage:[Utilities createMeme:bottextStr withImage:image atTop:NO]];
//        [imageView setImage:[Utilities createMeme:toptextStr withImage:imageView.image atTop:YES]];

    }
    return YES;
}

@end
